using System;

namespace Umbraco.Core.Events
{
	/// <summary>
	/// Extension methods for cancellable event operations
	/// </summary>
	public static class EventExtensions
	{
		/// <summary>
		/// Raises the event and returns a boolean value indicating if the event was cancelled
		/// </summary>
		/// <typeparam name="TSender"></typeparam>
		/// <typeparam name="TArgs"></typeparam>
		/// <param name="eventHandler"></param>
		/// <param name="args"></param>
		/// <param name="sender"></param>
		/// <returns></returns>
		public static bool IsRaisedEventCancelled<TSender, TArgs>(
			this TypedEventHandler<TSender, TArgs> eventHandler,
			TArgs args,
			TSender sender)
			where TArgs : CancellableEventArgs
		{
			if (eventHandler != null)
				eventHandler(sender, args);

			return args.Cancel;
		}

		/// <summary>
		/// Raises the event
		/// </summary>
		/// <typeparam name="TSender"></typeparam>
		/// <typeparam name="TArgs"></typeparam>
		/// <param name="eventHandler"></param>
		/// <param name="args"></param>
		/// <param name="sender"></param>
		public static void RaiseEvent<TSender, TArgs>(
			this TypedEventHandler<TSender, TArgs> eventHandler,
			TArgs args,
			TSender sender)
			where TArgs : EventArgs
		{
			if (eventHandler != null)
				eventHandler(sender, args);
		}
	}
}